﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Localization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Resource;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebReport.Models;

namespace WebReport.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IConfiguration _configuration;

        public HomeController(ILogger<HomeController> logger, IConfiguration configuration)
        {
            _logger = logger;
            _configuration = configuration;
        }

        public IActionResult Index()
        {
            var LanguageCookie = Request.Cookies["culture"];
            string culture = string.IsNullOrEmpty(LanguageCookie) ? Const.CNLANGUAGE : LanguageCookie;
            Response.Cookies.Append(
                CookieRequestCultureProvider.DefaultCookieName,
                CookieRequestCultureProvider.MakeCookieValue(new RequestCulture(culture)),
                new CookieOptions { Expires = DateTimeOffset.UtcNow.AddMonths(1) }
            );
            Thread.CurrentThread.CurrentCulture = new CultureInfo(culture);
            Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture);

            return View();
        }

        public IActionResult Report()
        {
            ViewBag.Type = Request.Query["type"].ToString().ToLower();
            string scheme = HttpContext.Request.Scheme;
            ViewBag.Address = $@"{scheme}://{HttpContext.Request.Host.Value}/Report/{ViewBag.Type}";

            //ViewBag.MyContent = $@"<iframe id = 'Frame' name = 'preview' border = '0' marginwidth = '0' marginheight = '0' frameBorder = '0' scrolling = 'no' style = 'width:100%;height:100%;' src = 'http://localhost/' ></iframe >";
            //ViewBag.Port = _configuration["Dashboard:WebAPI"];
            //HttpContext.Connection.RemoteIpAddress.ToString()
            //HttpContext.Connection.LocalPort
            //string url = new UrlShortener().GetShortUrl("http://192.168.1.4/QrCode/qrcode.html?data=dssdssdffddfdf");
            //string endstr = string.Empty;
            //string plainText = "admin";
            //using (SHA1 sha1 = new SHA1Managed())
            //{
            //    var bytes = Encoding.UTF8.GetBytes(plainText);
            //    var hash = sha1.ComputeHash(bytes);
            //    var builder = new StringBuilder();
            //    foreach (var b in hash)
            //    {
            //        builder.AppendFormat("{0:x2}", b);
            //    }
            //    endstr = builder.ToString();
            //}
            return View();
        }

        public static void requestPay()
        {
            string endstr = string.Empty;
            string plainText = "admin";
            using (SHA1 sha1 = new SHA1Managed())
            {
                var bytes = Encoding.UTF8.GetBytes(plainText);
                var hash = sha1.ComputeHash(bytes);
                var builder = new StringBuilder();
                foreach (var b in hash)
                {
                    builder.AppendFormat("{0:x2}", b);
                }
                endstr = builder.ToString();
            }

            Dictionary<string, string> dics = new Dictionary<string, string>();
            dics.Add("amount", "");
            dics.Add("callback_url", "");
            dics.Add("goodsname", "");
            dics.Add("merchno", "");
            dics.Add("notify_url", "");
            dics.Add("ordno", "");
            dics.Add("organno", "");
            dics.Add("version", "");
            dics.Add("paytype", "");
            getParamSrc(dics);
        }

        public static String getParamSrc(Dictionary<string, string> paramsMap)
        {
            var vDic = (from objDic in paramsMap orderby objDic.Key ascending select objDic);
            StringBuilder str = new StringBuilder();
            foreach (KeyValuePair<string, string> kv in vDic)
            {
                string pkey = kv.Key;
                string pvalue = kv.Value;
                str.Append(pkey + "=" + pvalue + "&");
            }
            String result = str.ToString().Substring(0, str.ToString().Length - 1);
            return result;
        }

        public IActionResult Error()
        {
            return View();
        }

        //public IActionResult Privacy()  
        //{
        //    return View();
        //}

        //[ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        //public IActionResult Error()
        //{
        //    return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        //}
    }
}
